package com.minyon_web_admin.common.utils;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.minyon_web_admin.model.SysUserBean;
public class PasswordHelper {
	protected final  static Logger minYonlogger = (Logger) LoggerFactory
			.getLogger(PasswordHelper.class);

	 
	//@Value("${password.algorithmName}")
	private static String algorithmName="md5";
	//@Value("${password.hashIterations}")
	private static int hashIterations=2;

	public static void encryptPassword(SysUserBean user) {
		RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
		String salt = randomNumberGenerator.nextBytes().toHex();//76d1c56a9508fc5ec62d0e287b046c2c
		user.setSalt(salt);
		String newPassword = new SimpleHash( algorithmName,  //加密算法  
                user.getPassword(),      //密码  
                ByteSource.Util.bytes(user.getLoginName()+salt),  //salt盐   username + salt  
                hashIterations   //迭代次数  
                ).toHex();  
        user.setPassword(newPassword); //94b1dbf4396344a7134e77f26e0118a6
        minYonlogger.info("当前用户加密信息  密码：" + newPassword+"  盐值:"+salt);
	}
 

	public static String  encryptPassword2(String name,String pwd,String salt) {
		String newPassword = new SimpleHash( algorithmName,  //加密算法  
				pwd,      //密码  
                ByteSource.Util.bytes(salt),  //salt盐   username + salt  
                hashIterations   //迭代次数  
                ).toHex();  
         
        minYonlogger.info("当前用户加密信息  密码：" + newPassword+"  盐值:"+salt);
        return newPassword;
	}
}
